package com.boodaba.todocarts.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import roboguice.activity.GuiceActivity;
import roboguice.inject.InjectExtra;
import roboguice.inject.InjectView;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.boodaba.todocarts.R;
import com.boodaba.todocarts.app.ILabelTodosActivity;
import com.boodaba.todocarts.app.LabelTodosPresenter;
import com.google.inject.Inject;

public class LabelTodosActivity extends GuiceActivity implements ILabelTodosActivity {
	
	@Inject protected LabelTodosPresenter mPresenter;
	@InjectView(android.R.id.list) protected ListView mTodosListView;
	@InjectView(android.R.id.empty) protected TextView mTodosEmptyTextView;
	@InjectView(R.id.bar_text) protected TextView mBarTitle;
	@InjectExtra(value="labelTitle") protected String mTargetLabelTitle;
	
	protected String mSelectedTodoTitle;
	protected View mSelectedTodoView;
	protected Map<String, Boolean> mTodoTitlesMap;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_label_todos);
		mPresenter.initializeWith(this);
		mTodosListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
				mSelectedTodoTitle = (String) adapter.getItemAtPosition(position);
				mSelectedTodoView = v;
				mPresenter.processTodo();
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.options_menu_label_todos, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menu_setting_todos:
				mPresenter.startSettingTodos();
				return true;
			case R.id.menu_refresh:
				mPresenter.refresh();
				return true;
		}
		return false;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		mPresenter.returnTo();	
	}

	public String getSelectedTodoTitle() {
		return mSelectedTodoTitle;
	}

	public String getTargetLabelTitle() {
		return mTargetLabelTitle;
	}
	
	public void setLabelTitle(String labelTitle) {
		mBarTitle.setText(labelTitle);		
	}

	public void setTodoList(Map<String, Boolean> todoList) {
		mTodoTitlesMap = todoList;
		TodoTitlesAdapter adapter = new TodoTitlesAdapter(
				this,
				R.layout.list_item_simple,
				new ArrayList<String>(mTodoTitlesMap.keySet()));
		mTodosListView.setAdapter(adapter);
		if (mTodoTitlesMap.size() == 0) {
			mTodosEmptyTextView.setVisibility(View.VISIBLE);
			return;
		}
		if (mTodosEmptyTextView.getVisibility() == View.VISIBLE) {
			mTodosEmptyTextView.setVisibility(View.GONE);
		}
	}
	
	public void setTodoMarked(String todoTitle) {
		mTodoTitlesMap.put(todoTitle, true);
		markTextView((TextView)mSelectedTodoView, true);
	}

	public void setTodoUnmarked(String todoTitle) {
		mTodoTitlesMap.put(todoTitle, false);
		markTextView((TextView)mSelectedTodoView, false);
	}

	public void startSettingTodos(String labelTitle) {
		Intent i = new Intent(this, TodoSettingsActivity.class);
		i.putExtra("labelTitle", labelTitle);
		startActivityForResult(i, 0);
	}

	private void markTextView(TextView tv, Boolean strikeFlag) {
		boolean notStriked = (tv.getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) == 0;

		if (strikeFlag && notStriked) {
			tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
			return;
		}
		if (!(strikeFlag || notStriked)) {
			tv.setPaintFlags(tv.getPaintFlags() ^ Paint.STRIKE_THRU_TEXT_FLAG);
		}
	}
	
	private class TodoTitlesAdapter extends ArrayAdapter<String> {
		
		private int mViewResId;
		private List<String> mTitles;
		
		public TodoTitlesAdapter(Context context, int textViewResourceId, List<String> objects) {
			super(context, textViewResourceId, objects);
			mViewResId = textViewResourceId;
			mTitles = objects;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			
			if (v == null) {
				v = getLayoutInflater().inflate(mViewResId, null);
			}
			
			TextView tv = (TextView) v.findViewById(R.id.todo_title);
			final String title = mTitles.get(position);
			tv.setText(title);
			markTextView(tv, mTodoTitlesMap.get(title));
			return v;
		}
	}
}